package com.android.pagemarket.ui.widget;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.android.pagemarket.R;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-1
 * @see : 播放视频弹出框
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class VideoPopWindow implements OnPreparedListener,OnCompletionListener
{
    private static final String TAG = "VideoPopWindow";
    private Activity mContext;
    private SurfaceView surfaceView;
    private MediaPlayer mediaPlayer;
    private Display  currDisplay;
    private OnClickListener listener;
    
    public VideoPopWindow(Activity context, OnClickListener listener)
    {
        mContext = context;
        this.listener = listener;
        initViews();
    }
    
    private void initViews()
    {
        surfaceView =(SurfaceView)mContext.findViewById(R.id.video_surface);
        currDisplay = mContext.getWindowManager().getDefaultDisplay();
               
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnCompletionListener(this);
        
        //设置SurfaceView不维护自己的缓冲区，而是等待屏幕的渲染引擎将内容推送到用户面前
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //对surface对象的状态进行监听
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback()
        {
            @Override
            public void surfaceCreated(SurfaceHolder holder)
            {
                Log.d(TAG, "--> surfaceCreated");
                play();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
            {
                
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder)
            {
                Log.d(TAG, "--> surfaceDestroyed");
                if(mediaPlayer != null)
                {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
            }
        });       
    }
    
    private void play()
    {
        try
        {            
            // 重置为初始状态
            mediaPlayer.reset();
            // 设置音乐流的类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(mContext, Uri.parse("android.resource://" + mContext.getPackageName() + "/raw/welcome"));
            // 设置video影片以SurfaceView Holder播放
            mediaPlayer.setDisplay(surfaceView.getHolder());
            // 开始播放
            mediaPlayer.prepare();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public void pause()
    {
        if(mediaPlayer == null || !mediaPlayer.isPlaying()) return;
        mediaPlayer.pause();
    }
    
    void adjustVideoView(int vWidth, int vHeight)
    {
        // 320
        int dWidth  = currDisplay.getWidth();
        // 480
        int dHeight = currDisplay.getHeight();
        
        int sWidth = 0;
        int sHeight = 0;
        
        if(vWidth >= dWidth)
        {
            sWidth = dWidth;
            if(vHeight >= dHeight)
            {
                sHeight = dHeight;
            }else
            {
                sHeight = vHeight;
            }
        }else
        {
            sWidth = vWidth;
            if(vHeight >= dHeight)
            {
                sHeight = dHeight;
            }else
            {
                sHeight = vHeight;
            }
        }
        RelativeLayout.LayoutParams lp = (LayoutParams) surfaceView.getLayoutParams();
        lp.width = sWidth;
        lp.height = sHeight;
        surfaceView.setLayoutParams(lp);
        mContext.getWindow().getDecorView().requestLayout();
    }

    @Override
    public void onPrepared(MediaPlayer mp)
    {
        mp.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        if (listener != null)
        {
            listener.onClick(null);
        }
    }
}